package com.baozun.houji.order.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baozun.houji.order.network.orderApiService
import com.baozhun.mall.common.model.bean.ExpressInfoBean
import com.baozun.houji.order.R
import com.baozun.houji.order.model.bean.LogisticsCompanyInfoBean
import com.baozun.houji.order.model.bean.RefundDetailBean
import com.blankj.utilcode.util.ToastUtils
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/20
 * @Version：1.0.0
 */
class RefundDetailViewModel : BaseViewModel() {
    var mSurplusTime = MutableLiveData<Long>()
    var mRefundDetail = MutableLiveData<RefundDetailBean>()
    var currentExpressInfoBean = MutableLiveData<ExpressInfoBean>()
    var expressCompany = MutableLiveData<String>()
    var expressCompanyCode: String = ""
    var expressNumber = MutableLiveData<String>()
    var mLogisticsList = arrayListOf<LogisticsCompanyInfoBean>()
    var mExpressInfoBean = MutableLiveData<ExpressInfoBean>()

    /**
     * 獲取售後详情
     */
    fun getRefundDetail(afterSaleNo: String) {
        request({ orderApiService.getRefundDetail(afterSaleNo) }, {
            mRefundDetail.value = it
        }, isShowDialog = false)
    }

    /**
     * 提交物流信息
     */
    fun delivery(afterSaleNo: String, submitSuccessCallBack: () -> Unit) {
        request({ orderApiService.delivery(afterSaleNo, expressCompanyCode, expressNumber.value.toString()) }, {
            submitSuccessCallBack.invoke()
            ToastUtils.showShort(com.baozhun.mall.res.R.string.submit_success)
        }, isShowDialog = true)
    }

    /**
     * 获取物流公司信息
     */
    fun getLogisticsCompany(callBack: ((ArrayList<LogisticsCompanyInfoBean>) -> Unit)? = null) {
        if (mLogisticsList.isNotEmpty()) {
            callBack?.invoke(mLogisticsList)
            return
        }
        request({ orderApiService.getLogisticsCompany() }, {
            mLogisticsList = it
            callBack?.invoke(it)
        }, isShowDialog = false)
    }

    /**
     * 获取物流信息
     */
    fun getLogisticsInfo(afterSaleNo: String) {
        request({ orderApiService.getRefundLogisticsInfo(afterSaleNo) }, {
            mExpressInfoBean.value = it
        }, isShowDialog = false)
    }

}